package com.platinadesign.axa.loader
{
	import com.platinadesign.axa.event.ResourceEvent;
	import com.platinadesign.axa.view.Page;
	import com.platinadesign.axa.vo.ConfigueData;
	import com.platinadesign.axa.vo.Resource;
	import com.platinadesign.axa.vo.ResourceLoadingVO;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;

	public class ResourceLoader extends EventDispatcher
	{

		public function get totalNum() : int
		{
			return _totalNum;
		}

		private var loader : Loader;

		private var _numberComplete : int;

		private var _totalNum : int;

		private var _pagesURL : Vector.<String>;

		private var _contentsArr : Vector.<Page>;

		public function ResourceLoader() : void
		{
			_contentsArr = new Vector.<Page>();
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, resourceLoadedHandler );
			loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
		}

		public function loadResource( configure : ConfigueData ) : void
		{
			_numberComplete = 0;
			_totalNum = configure.numberPages;
			_pagesURL = configure.pagesURLArr;
			doLoad();
		}

		protected function ioErrorHandler( event : IOErrorEvent ) : void
		{
			this.dispatchEvent( new ResourceEvent( ResourceEvent.RESOURCE_LOAD_ERROR, generateLoadingVO()));
			_numberComplete++;
			doLoad();
		}

		protected function resourceLoadedHandler( event : Event ) : void
		{
			const content : DisplayObject = loader.content;
			//只有被成功下载的页面才会被顺序排列，所以用_contentsArr.length来当做序列号，而不是_numberComplete。
			_contentsArr.push( createPage( content, _contentsArr.length ));
			// this event will be used for making progress bar...
			this.dispatchEvent( new ResourceEvent( ResourceEvent.RESOURCE_LOADED, generateLoadingVO()));
			_numberComplete++;
			doLoad();
		}

		private function generateLoadingVO() : ResourceLoadingVO
		{
			return new ResourceLoadingVO( _numberComplete, _totalNum, getURL());
		}

		private function getURL() : String
		{
			return ( _numberComplete < _totalNum ) ? _pagesURL[ _numberComplete ] : "complete";
		}

		private function doLoad() : void
		{
			// if downloaded all then dispatch event to note out side ,my job done,dont care who will take over the result.
			if( _numberComplete < _totalNum )
			{
				loader.load( new URLRequest( getURL()),
					new LoaderContext( false, ApplicationDomain.currentDomain )) //, SecurityDomain.currentDomain ));

			}
			else
			{
				var resourceEvent : ResourceEvent = new ResourceEvent( ResourceEvent.ALL_RESOURCE_LOADED, generateLoadingVO());
				resourceEvent.resource = new Resource( _contentsArr );
				this.dispatchEvent( resourceEvent );
			}
		}

		/**
		 * 这个方法本来可以没有的，但是为了让你明白什么叫做工厂方法，通常生成实例的方法叫做工厂方法，他通常包含根据属性创建对象的特性
		 * to generate function comment , press ctrl+shift+d....i d be better off to know this.
		 * @param content
		 * @param index
		 * @return
		 */
		private function createPage( content : DisplayObject, index : uint ) : Page
		{
			const page : Page = new Page( content, index ,(index == 0) || (_numberComplete == _totalNum - 1));
			return page;
		}
	}
}